import 'package:flutter/material.dart';
import 'package:wan_android/api/search.dart';
import 'package:wan_android/model/search/search_hot_type.dart';
import 'package:wan_android/model/search/search_list_type.dart';

class SearchPageModel extends ChangeNotifier {
  int page = 0;
  
  List<SearchHotItem> _hotList = [];
  List<SearchListItem> _searchList = [];

  List<SearchHotItem> get hotList => _hotList;
  List<SearchListItem> get searchList => _searchList;

  Future getHotList() async {
    Map<String, dynamic> response = await getHotKeyList();
    SearchHotResp searchHotResp = SearchHotResp.fromJson(response);
    if (searchHotResp.data != null) {
      _hotList = searchHotResp.data;
    } else {
      _hotList = [];
    }
    notifyListeners();
  }

  Future getSearchList(data, { bool? loadMore = false }) async {
    if (loadMore!) {
      page++;
      Map<String, dynamic> response = await getSearchData(data, page: page);
      SearchListResp searchListResp = SearchListResp.fromJson(response);

      if (searchListResp.data != null && searchListResp.data.datas != null) {
        _searchList.addAll(searchListResp.data.datas);
      } else {
        _searchList = [];
      }
    } else {
      page = 0;
      Map<String, dynamic> response = await getSearchData(data, page: page);
      SearchListResp searchListResp = SearchListResp.fromJson(response);

      _searchList.clear();
      if (searchListResp.data != null && searchListResp.data.datas != null) {
        _searchList = searchListResp.data.datas;
      } else {
        _searchList = [];
      }
      notifyListeners();
    }

  }
}